//
//  NSFileManager+JSKit.m
//  JSBasicFramework
//
//  Created by Apple on 2018/12/5.
//  Copyright © 2018 Apple. All rights reserved.
//

#import "NSFileManager+JSKit.h"

@implementation NSFileManager (JSKit)

+(NSString *)getSandboxSpecifyWithType:(DirectoryType) type{
    NSString *path = nil;
    switch (type) {
        case DirectoryTypeMainBundle:{
            path = [[NSBundle mainBundle] bundlePath];
            break;
        }
        case DirectoryTypeDocuments:{
            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
            path = [paths objectAtIndex:0];
            break;
        }
        case DirectoryTypeLibrary:{
            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
            path = [paths objectAtIndex:0];
            break;
        }
        case DirectoryTypePreferences:{
            
            break;
        }
        case DirectoryTypeCache:{
            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES);
            path = [paths objectAtIndex:0];
            break;
        }
        case DirectoryTypeTmp:{
            path = NSTemporaryDirectory();
            break;
        }
    }
    return path;
}

+(NSString *)creatSandboxSpecifyWithType:(DirectoryType) type subDirectory:(NSString *)fileName {
    NSAssert(fileName, @"不能为空");
//    NSString *template = [fileName componentsSeparatedByString:@"/"][0];
//    NSAssert(!template.length, @"fileName 格式错误");
    NSString *folderPath = [[self getSandboxSpecifyWithType:type] stringByAppendingPathComponent:fileName];
    BOOL success = [[NSFileManager defaultManager] fileExistsAtPath:folderPath];
    if(!success){
       success = [[NSFileManager defaultManager] createDirectoryAtPath:folderPath
                                  withIntermediateDirectories:NO
                                                   attributes:nil
                                                        error:nil];
       
    }
    return success ? folderPath : nil;
}
+(NSArray *)getSandboxSepcifySubFolderWithType:(DirectoryType) type subPath:(nullable NSString *)subPath fileType:(JSFileType) fileType{
    NSString *path = [self getSandboxSpecifyWithType:type];
    if (subPath.length) {
        path = [path stringByAppendingString:subPath];
        if (![[NSFileManager defaultManager] fileExistsAtPath:path]) {
            return nil;
        }
    }
    NSArray *template = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:nil];
    NSMutableArray *folders = @[].mutableCopy;
    for (NSString *string in template) {
        NSString *p = [path stringByAppendingString:string];
        if ([self judgeFileTypeSpecialPath:p] == fileType) {
            [folders addObject:string];
        }
       /* BOOL isDir = NO;
        [[NSFileManager defaultManager] fileExistsAtPath:p isDirectory:&isDir];
        if (fileType == FileTypeFolder && isDir) {
            [folders addObject:string];
        }
        if (fileType == FileTypeFile && !isDir) {
            [folders addObject:string];
        }*/
    }
    return folders;
}

+(BOOL)removeSandboxSepcifySubFolderWithType:(DirectoryType) type subPath:(NSString *)subPath{
    NSAssert(subPath.length, @"不能为空");
    NSString *path = [self getSandboxSpecifyWithType:type];
    path = [path stringByAppendingString:subPath];
    if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
        return [[NSFileManager defaultManager] removeItemAtPath:path error:nil];
    }
    return NO;
}

+(BOOL)moveItemAtPathFormDirectoryType:(DirectoryType) atType
                             atsubPath:(NSString *) atSubPath
                       toDirectoryType:(DirectoryType) toType
                             tosubPath:(nullable NSString *) toSubPath{
    NSAssert(atSubPath.length, @"不能为空");
    NSString *toPath = [self getSandboxSpecifyWithType:toType];
    NSString *atPath = [[self getSandboxSpecifyWithType:atType] stringByAppendingString:atSubPath];
    if ([self judgeFileTypeSpecialPath:atPath] != FileTypeFile) {
        return NO;
    }
    if (toSubPath) {
        toPath = [toPath stringByAppendingString:toSubPath];
    }
    switch ([self judgeFileTypeSpecialPath:toPath]) {
        case FileUnkwon:{
             // create
            BOOL success = [[NSFileManager defaultManager] createDirectoryAtPath:toPath
                                                      withIntermediateDirectories:NO
                                                                       attributes:nil
                                                                            error:nil];
            if (!success) {
                return NO;
            }
            break;
        }
        case FileTypeFolder:
            break;
        default:
            return NO;
            break;
    }
    NSError *error = nil;
    BOOL success = [[NSFileManager defaultManager] moveItemAtPath:atPath toPath:toPath error:&error];
    return success;
}

+(JSFileType)judgeFileTypeSpecialPath:(NSString *) path{
    BOOL isDir = NO;
    if ([[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isDir] ) {
        return isDir ? FileTypeFolder : FileTypeFile;
    }
    return FileUnkwon;
}

+(BOOL)clearLocaCacheAtPath:(NSString *)path{
   return [[NSFileManager defaultManager] removeItemAtPath:path error:nil];
}
@end
